<template>
  <el-container class="main-container">
    <!-- 头部区域 -->
    <el-header>
      <!-- 左侧的 logo -->
      <img src="../../assets/images/logo.png" alt="" />
      <!-- 右侧的菜单 -->
      <el-menu
        class="el-menu-top"
        mode="horizontal"
        background-color="#23262E"
        text-color="#fff"
        active-text-color="#409EFF"
      >
        <el-submenu index="1">
          <template slot="title">
            <!-- 头像 -->
            <img :src="userInfo.user_pic" alt="" class="avatar" v-if='userInfo.user_pic'/>
            <img src="../../assets/logo.png" alt="" class="avatar" v-else/>

            <!-- 用户昵称 -->
            <span>{{userInfo.nickname || userInfo.username}}</span>

          </template>
          <el-menu-item index="1-1"><i class="el-icon-s-operation"></i>基本资料</el-menu-item>
          <el-menu-item index="1-2"><i class="el-icon-camera"></i>更换头像</el-menu-item>
          <el-menu-item index="1-3"><i class="el-icon-key"></i>重置密码</el-menu-item>
        </el-submenu>
        <el-menu-item index="2" @click="clickLogout"><i class="el-icon-switch-button"></i>退出</el-menu-item>
      </el-menu>
    </el-header>
    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside width="200px">
        <el-menu
          :default-active="$route.path"
          background-color="#23262E"
          text-color="#fff"
          active-text-color="#409EFF"
          unique-opened
          router
        >
          <template v-for="item in menus" >
            <!-- 不包含子菜单的“一级菜单” -->
            <el-menu-item :index="item.indexPath" :key='item.indexPath' v-if="!item.children">
              <i class="el-icon-s-tools"></i>{{item.title}}
            </el-menu-item>
            <!-- 包含子菜单的“一级菜单” -->
            <el-submenu :index="item.indexPath" :key='item.indexPath' v-else>
              <template slot="title">
                <i class="el-icon-s-tools"></i>
                <span>{{item.title}}</span>
              </template>
              <el-menu-item :index="subItem.indexPath" v-for="subItem in item.children" :key="subItem.indexPath">
                <i class="el-icon-star-on"></i>{{subItem.title}}
              </el-menu-item>
            </el-submenu>
          </template>
        </el-menu>
      </el-aside>
      <el-container>
        <!-- 页面主体区域 -->
        <el-main>
          <!-- 二级路由坑 -->
          <router-view></router-view>
        </el-main>
        <!-- 底部 footer 区域 -->
        <el-footer>© www.itheima.com - 黑马程序员</el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
// 进入到后台的首页,应该获取用户的名称信息与头像信息
import { mapState } from 'vuex'

export default {
  name: 'MainIndex',
  data () {
    return {
      menus: []
    }
  },
  created () {
    // 路由时动态参数  $route.query   $route.params
    // console.log(this.$route)
    if (this.token) {
      // 有token 正常放行
      // 触发initUserInfo方法获取用户的数据
      this.$store.dispatch('initUserInfo')

      // 进入页面获取菜单
      this.initMenus()
    } else {
      // 没有token 去登录页
      this.$router.push('/login')
    }
  },
  computed: {
    ...mapState(['userInfo', 'token'])
  },
  methods: {
    // 退出功能
    async clickLogout () {
      // 用户点击了退出,应该给一个提示,是否真的退出(进行确认)
      const res = await this.$confirm('确定要退出吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)

      if (res === 'cancel') return false

      // 点击确定,退出  清除token(vuex清除与localStorage清除)
      this.$store.commit('setToken', '')

      // 跳转登录页
      this.$router.push('/login')
    },

    // 获取菜单
    async initMenus () {
      const { data: res } = await this.$http.get('/my/menus')

      if (res.code === 0) {
        this.menus = res.data
      }
    }
  }
}
</script>

<style scoped lang='less'>
.main-container {
  height: 100%;
  .el-header,
  .el-aside {
    background-color: #23262e;
  }
  .el-header {
    padding: 0;
    display: flex;
    justify-content: space-between;
  }
  .el-main {
    overflow-y: scroll;
    height: 0;
    background-color: #F2F2F2;
  }
  .el-footer {
    background-color: #eee;
    font-size: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

.avatar {
  border-radius: 50%;
  width: 35px;
  height: 35px;
  background-color: #fff;
  margin-right: 10px;
  object-fit: cover;
}

.el-aside {
  .el-submenu,
  .el-menu-item {
    width: 200px;
    user-select: none;
  }
}
</style>
